package com.codefish.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codefish.dto.MenuTreeDto;
import com.codefish.entity.SysBaseCommonEntity;
import com.codefish.entity.SysPermissionEntity;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.SysPermissionService;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 公共字段
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-04-15 17:58:46
 */
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    /**
     * jwt
     */
    @Autowired
    private JWTService jwtService;
    /**
     * 权限
     */
    @Autowired
    private SysPermissionService permissionService;


    /**
     * 新增 Permission
     *
     * @param permission
     * @return
     */
    @PostMapping("/savePermission")
    public R savePermission(@RequestHeader("Authentication") String token, @RequestBody SysPermissionEntity permission) {
        // 查询权限是否已经存在
        int count = permissionService.selectCount(permission);
        if (count > 0) {
            return R.error("该权限已存在，请核实！");
        }

        // 获取用户
        SysUserTokenEntity user = jwtService.getUser(token);

        Long userId = user.getId();

        int save = permissionService.savePermission(userId, permission);

        if (save != 1) {
            return R.error("新增失败！");
        }

        return R.ok();
    }

    /**
     * 修改权限
     *
     * @param token
     * @param permission
     * @return
     */
    @PostMapping("/updatePermission")
    @ResponseBody
    public R updatePermission(@RequestHeader("Authentication") String token, @RequestBody SysPermissionEntity permission) {
        // 查询权限是否已经存在
        int count = permissionService.selectCount(permission);
        if (count > 0) {
            return R.error("该权限已存在，请核实！");
        }

        // 获取用户
        SysUserTokenEntity user = jwtService.getUser(token);

        Long userId = user.getId();

        int update = permissionService.updatePermission(userId, permission);

        if (update < 1) {
            return R.error("修改失败！");
        }
        return R.ok();
    }

    /**
     * 获取菜单
     *
     * @param token
     * @return
     */
    @GetMapping("/getMenuTree")
    @ResponseBody
    public R getMenuTree(@RequestHeader("Authentication") String token, @RequestParam Map<String, Object> params) {

        // 获取用户
        SysUserTokenEntity user = jwtService.getUser(token);

        Long userId = user.getId();

        List<MenuTreeDto> menuTree = permissionService.getMenuTree(userId, params);

        return R.ok().put("menuTree", menuTree);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestParam("id") String id) {
        SysPermissionEntity sysPermission = permissionService.getById(id);

        return R.ok().put("sysPermission", sysPermission);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("codefish:syspermission:save")
    public R save(@RequestBody SysPermissionEntity sysPermission) {
        permissionService.save(sysPermission);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("codefish:syspermission:update")
    public R update(@RequestBody SysPermissionEntity sysPermission) {
        permissionService.updateById(sysPermission);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("codefish:syspermission:delete")
    public R delete(String[] ids) {
        permissionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam(value = "id",required = true) String id) {

        int count = permissionService.count(new QueryWrapper<SysPermissionEntity>()
                .eq("id", id)
                .eq("is_del", 0));

        if (count <= 0) {
            return R.ok("删除成功！");
        }

        if (null == id || "".equals(id)) {
            return R.error("非法参数，请确认！");
        }

        boolean deleteFlag = permissionService.deleteOne(id);

        if (!deleteFlag) {
            return R.error("删除失败！");
        }

        return R.ok("删除成功！");
    }
    /**
     * 根据上级菜单获取下级菜单列表
     * @param key
     * @return
     */
    @GetMapping("/getListByMenuType")
    public R getListByMenuType (@RequestParam("key") String key){
        if (StringUtils.isEmpty(key)){
            return R.error("非法参数！");
        }
        List<SysPermissionEntity> menuList = permissionService.getListByMenuType(key);


        return R.ok().put("menuList",menuList);
    }

}
